package prefuse.vensim;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

import com.vensim.Vensim;

/**
 * Helper class for pangaea climate simulation service. It allows easy usage of
 * Vensim engine used for launching climate changes simulations.
 * 
 * @author janusz
 * 
 */
public class VensimHelp {

	private static final String V_LOAD_MODEL = "SPECIAL>LOADMODEL|";


	private static String transferVdfToTxt = "MENU>VDF2DLIST|C:\\tmp\\ann880Test.vdf";
	
	
	private static String[] varNameArray;
	/**
	 * Vensim instance.
	 */
	private Vensim vensim;

	/**
	 * Context id that will be used for all operations.
	 */
	private int ctxId;
	
	
	private String modelLocation;

	/**
	 * Initializes Vensim with passed lib name.
	 * 
	 * @param libName
	 *            name of dll that should be used
	 * @param modelPath
	 *            path to model that should be loaded
	 * @throws VensimException
	 *             when there is a problem with Vensim communication
	 */
	public VensimHelp(String libName, String modelPath) throws VensimException {
		vensim = new Vensim(libName);
		System.out.println(libName);
		if (vensim == null) {
			throw new RuntimeException(new Error("vensim wasn't properly initialized"));
		}
		modelLocation = modelPath;
	}

	/**
	 * Executes given command against Vensim core.
	 * 
	 * @param command
	 *            command that should be executed
	 * @throws VensimException
	 *             in case of any Vensim error
	 */
	private void doCommand(String command) throws VensimException {
		int ret = Vensim.CCommand(ctxId, command);

		if (ret == 0) {

			throw new VensimException("Error when executing command: "+ command);
		}
		System.out.println(ret);

	}

	/**
	 * Loads given model.
	 * 
	 * @param modelPath
	 *            path to model file that should be loaded
	 * @throws VensimException
	 *             in case of any Vensim error
	 */
	public String[] loadModelVarName() throws VensimException {
		doCommand(V_LOAD_MODEL + modelLocation);
		varNameArray= Vensim.get_varnames("*", 0);
		System.out.println("this is in loadModel: " + varNameArray.length);
		return varNameArray;
	}
	
	
	  /**
     * Runs the simulation.
     * @throws VensimException in case of any Vensim error
     */
    public void run() throws VensimException {
      //  validateContext();

    	Vensim.command("SIMULATE>SETVAL|FINAL TIME = 4.0");
        Vensim.command("SIMULATE>SETVAL|TIME STEP = 0.5");
    //    doCommand(V_RUN);
        Vensim.command( "MENU>RUN|O" );
       // System.out.println("it is hard" + getvalue);
    }
    
    
    public void transferVdfToTxt() throws VensimException{
    	transferVdfToTxt = "MENU>VDF2DLIST|C:\\tmp\\Test.vdf";
    	 doCommand(transferVdfToTxt);
    }

	public static void main(String argv[]) {

		try {
			VensimHelp VemsimController = new VensimHelp("VenDLL32","C:\\tmp\\WNVDecisionTree.vpm");
			VemsimController.loadModelVarName();
			VemsimController.run();
			VemsimController.transferVdfToTxt();
		} catch (VensimException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

}
